package edu.hubu.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import net.moon.jpa.model.BaseEntity;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.io.Serializable;
import java.util.List;

/**
 * @author moonlan
 * @date 2020/10/18 下午12:28
 */

@Entity
@Table(name = "course")
//逻辑删除注解，删除sql变成了update
@SQLDelete(sql = "update course set course_is_deleted = true where course_id = ?")
//where条件带上了逻辑删除条件
@Where(clause = "course_is_deleted = false")
@ApiModel("课程")
@JsonIgnoreProperties(value = {"hibernateLazyInitializer", "handler", "fieldHandler"})
public class Course extends BaseEntity implements Serializable {

//    private static final long  serialVersionUID=-4451341239174094573L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "snowflakeId")
    @GenericGenerator(name = "snowflakeId", strategy = "edu.hubu.commons.SnowflakeId", parameters = {@org.hibernate.annotations.Parameter(name = "prefix", value = "COURSE")})
    @Column(name = "course_id")
    @ApiModelProperty(value = "course的id", position = 1)
    private String courseId;

    @Column(name = "course_name")
    @ApiModelProperty(value = "课程名称", position = 2)
    private String courseName;

    @Column(name = "course_description")
    @ApiModelProperty(value = "课程简介", position = 8)
    private String courseDescription;

    @OneToOne(targetEntity = Book.class, cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
    @JoinColumn(name = "course_book_id", referencedColumnName = "book_id")
    @ApiModelProperty(value = "课程书", position = 8)
    private Book book;

    @Column(name = "course_is_forbidden")
    @ApiModelProperty(value = "课程是否被禁用", position = 12)
    private boolean courseIsForbidden = false;

    @Column(name = "course_is_deleted")
    @ApiModelProperty(value = "课程是否被删除", position = 13)
    private boolean courseIsDeleted = false;

    @OneToMany(mappedBy = "course")
    @ApiModelProperty(hidden = true)
    @JsonIgnore
    private List<TeacherCourse> teacherCourses;

    @OneToMany(mappedBy = "course")
    @ApiModelProperty(hidden = true)
    @JsonIgnore
    private List<StudentCourse> studentCourses;

    public Course(String courseId, String courseName, String courseDescription, Book book, boolean courseIsForbidden, boolean courseIsDeleted, List<TeacherCourse> teacherCourses, List<StudentCourse> studentCourses) {
        this.courseId = courseId;
        this.courseName = courseName;
        this.courseDescription = courseDescription;
        this.book = book;
        this.courseIsForbidden = courseIsForbidden;
        this.courseIsDeleted = courseIsDeleted;
        this.teacherCourses = teacherCourses;
        this.studentCourses = studentCourses;
    }

    public Course() {
    }

    public static CourseBuilder builder() {
        return new CourseBuilder();
    }

    public String getCourseId() {
        return this.courseId;
    }

    public String getCourseName() {
        return this.courseName;
    }

    public String getCourseDescription() {
        return this.courseDescription;
    }

    public Book getBook() {
        return this.book;
    }

    public boolean isCourseIsForbidden() {
        return this.courseIsForbidden;
    }

    public boolean isCourseIsDeleted() {
        return this.courseIsDeleted;
    }

    public List<TeacherCourse> getTeacherCourses() {
        return this.teacherCourses;
    }

    public List<StudentCourse> getStudentCourses() {
        return this.studentCourses;
    }

    public void setCourseId(String courseId) {
        this.courseId = courseId;
    }

    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }

    public void setCourseDescription(String courseDescription) {
        this.courseDescription = courseDescription;
    }

    public void setBook(Book book) {
        this.book = book;
    }

    public void setCourseIsForbidden(boolean courseIsForbidden) {
        this.courseIsForbidden = courseIsForbidden;
    }

    public void setCourseIsDeleted(boolean courseIsDeleted) {
        this.courseIsDeleted = courseIsDeleted;
    }

    public void setTeacherCourses(List<TeacherCourse> teacherCourses) {
        this.teacherCourses = teacherCourses;
    }

    public void setStudentCourses(List<StudentCourse> studentCourses) {
        this.studentCourses = studentCourses;
    }

    public static class CourseBuilder {
        private String courseId;
        private String courseName;
        private String courseDescription;
        private Book book;
        private boolean courseIsForbidden;
        private boolean courseIsDeleted;
        private List<TeacherCourse> teacherCourses;
        private List<StudentCourse> studentCourses;

        CourseBuilder() {
        }

        public CourseBuilder courseId(String courseId) {
            this.courseId = courseId;
            return this;
        }

        public CourseBuilder courseName(String courseName) {
            this.courseName = courseName;
            return this;
        }

        public CourseBuilder courseDescription(String courseDescription) {
            this.courseDescription = courseDescription;
            return this;
        }

        public CourseBuilder book(Book book) {
            this.book = book;
            return this;
        }

        public CourseBuilder courseIsForbidden(boolean courseIsForbidden) {
            this.courseIsForbidden = courseIsForbidden;
            return this;
        }

        public CourseBuilder courseIsDeleted(boolean courseIsDeleted) {
            this.courseIsDeleted = courseIsDeleted;
            return this;
        }

        public CourseBuilder teacherCourses(List<TeacherCourse> teacherCourses) {
            this.teacherCourses = teacherCourses;
            return this;
        }

        public CourseBuilder studentCourses(List<StudentCourse> studentCourses) {
            this.studentCourses = studentCourses;
            return this;
        }

        public Course build() {
            return new Course(courseId, courseName, courseDescription, book, courseIsForbidden, courseIsDeleted, teacherCourses, studentCourses);
        }

        public String toString() {
            return "Course.CourseBuilder(courseId=" + this.courseId + ", courseName=" + this.courseName + ", courseDescription=" + this.courseDescription + ", book=" + this.book + ", courseIsForbidden=" + this.courseIsForbidden + ", courseIsDeleted=" + this.courseIsDeleted + ", teacherCourses=" + this.teacherCourses + ", studentCourses=" + this.studentCourses + ")";
        }
    }
}
